package com.ydj.sync.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.SimpleType;
import com.ydj.framework.config.JsonConfig;
import com.ydj.framework.exception.CommonException;
import com.ydj.framework.protocol.JsonBaseRequest;
import com.ydj.framework.protocol.JsonBaseResponse;
import com.ydj.framework.protocol.JsonResult;
import com.ydj.framework.utils.OkHttpClientUtils;
import com.ydj.framework.utils.SpringContextUtil;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

public class MockClient {

    private MockMvc mockMvc;
    private String baseUrl;

    private String token;

    public MockClient(MockMvc mockMvc, String baseUrl, String token) {
        this.mockMvc = mockMvc;
        this.baseUrl = baseUrl;
        if (!baseUrl.endsWith("/")) {
            this.baseUrl += "/";
        }
        this.token = token;
    }

    public <T extends JsonBaseResponse> T postJson(String relativeUrl, JsonBaseRequest req,  Class<T> clazz) throws Exception {
        String url = baseUrl;
        if (relativeUrl.startsWith("/")) {
            relativeUrl = relativeUrl.substring(1);
        }
        url += relativeUrl;

        System.out.println("测试接口 " + url);

        ObjectMapper mapper = getJsonMapper();
        String body = mapper.writeValueAsString(req);
        System.out.println("发送请求=" + body);

        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post(url)
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content(body);
        if (token != null) {
            builder.header("Authorization", token);
        }

        MvcResult result = mockMvc.perform(builder)
                .andExpect(status().isOk())
                .andReturn();
        String resString = result.getResponse().getContentAsString();
        System.out.println("返回响应=" + resString);

        JSONObject jsonResult = new JSONObject(resString);
        if (jsonResult.getInt("code") != 0) {
            CommonException.throwException("返回的code不等于0, 返回内容是=" + resString);
        }

        if (clazz == null) {
            return null;
        }
        JSONObject dataObj = jsonResult.optJSONObject("data");
        if (dataObj == null) {
            return null;
        }
        String strData = dataObj.toString();
        return mapper.readValue(strData, clazz);
    }

    private ObjectMapper getJsonMapper() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        return new JsonConfig().jacksonObjectMapper(builder);
    }
}
